package com.six.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.bean.Sale;
import com.six.service.SaleService;
import com.six.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

    @RestController
    @RequestMapping("sale")
    @CrossOrigin
    public class SaleController {
        @Autowired
        SaleService saleService;

        @GetMapping("list")
        public Result getList() {
            List<Sale> list = saleService.list();
            return Result.success(list);
        }

        @GetMapping("page")
        public Result getPage(Page page) {
            IPage result = saleService.page(page);
            return Result.success(result);
        }

        @PostMapping
        public Result add(@RequestBody Sale sale) {
            saleService.save(sale);
            Sale sale1 = saleService.getById(sale.getSid());
            return Result.success("保存成功", sale1);
        }

        @DeleteMapping("/{sid}")
        public Result delete(@PathVariable("sid") int sid) {
            boolean b = saleService.removeById(sid);
            if (b) {
                return Result.success("删除成功");
            } else {
                return Result.fail(201, "删除失败");
            }
        }

        @PutMapping
        public Result edit(@RequestBody Sale sale) {
            boolean b = saleService.saveOrUpdate(sale);
            Sale sale1 = saleService.getById(sale.getSid());
            if (b) {
                return Result.success("保存成功", sale1);
            } else {
                return Result.fail(201, "修改失败");
            }
        }
    }


